package com.cac.demo.newCode.sorting.utils;

import com.cac.demo.newCode.cemop.dto.CemopLogDTO;
import com.cac.demo.newCode.sorting.dto.LocalLogDTO;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
@Component
public class LocalLogUtil {

    @Resource
    private Environment environment;
    public  List<LocalLogDTO> executeSqlQuery(String sql){
        Connection connection = null;
        try {
            // 连接到SQLite数据库，数据库文件是test.db，如果文件不存在，会自动创建
            String sqlUrl=environment.getProperty("cemopOrLocalLog.linuxFiledDir");
            connection = DriverManager.getConnection(sqlUrl);
            // 接下来你可以使用connection对象来执行SQL语句，例如创建表、查询数据等
            Statement statement = connection.createStatement();
            statement.execute("PRAGMA journal_mode = WAL;");
            ResultSet resultSet = statement.executeQuery(sql);
            ArrayList<LocalLogDTO> localLogDTOS = new ArrayList<>();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String level = resultSet.getString("level");
                String loggerName = resultSet.getString("logger_name");
                String message = resultSet.getString("message");
                String exception = resultSet.getString("exception");
                String createTime = resultSet.getString("create_time");
                String method = resultSet.getString("method");
                String ipAddress = resultSet.getString("ip_address");
                String requestParam = resultSet.getString("request_param");
                String responseParam = resultSet.getString("response_param");
                localLogDTOS.add(new LocalLogDTO(id, level, loggerName, message, exception, createTime, method, ipAddress, requestParam, responseParam));
            }
            return localLogDTOS;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    // 关闭连接
                    connection.close();
                }
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return null;
    }

    public String executeSqlCount(String sql) {
        Connection connection = null;
        try {
            // 连接到SQLite数据库，数据库文件是test.db，如果文件不存在，会自动创建
            String sqlUrl=environment.getProperty("cemopOrLocalLog.linuxFiledDir");
            connection = DriverManager.getConnection(sqlUrl);
            // 接下来你可以使用connection对象来执行SQL语句，例如创建表、查询数据等
            Statement statement = connection.createStatement();
            statement.execute("PRAGMA journal_mode = WAL;");
            ResultSet resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {
                return resultSet.getString("pageTotal");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    // 关闭连接
                    connection.close();
                }
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return null;
    }
}
